// 维护用户信息相关的数据 如token
export default {
  namespaced: true,
  // state: {
  //   token: '',
  //   address: {}
  // },
  state() {
    const addStr = uni.getStorageSync('address')
    const userInfoStr = uni.getStorageSync('userInfo')
    return {
      token: uni.getStorageSync('token_102_ugo') || '',
      userInfo: userInfoStr ? JSON.parse(userInfoStr) : {},
      address: addStr ? JSON.parse(addStr) : {},
      redirectInfo: null, // 记录回跳地址
    }
  },
  getters: {
    fullAddress(state) {
      const { provinceName, cityName, countyName, detailInfo } = state.address
      if (!provinceName) return ''
      // 拼接 省，市，区，详细地址 的字符串并返回给用户
      return provinceName + cityName + countyName + detailInfo
    },
  },
  mutations: {
    setAddress(state, address) {
      state.address = address
      // sync 同步   async  异步
      uni.setStorageSync('address', JSON.stringify(address))
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo

      uni.setStorageSync('userInfo', JSON.stringify(userInfo))
    },
    setToken(state, token) {
      state.token = token

      uni.setStorageSync('token_102_ugo', token)
    },
    setRedirectInfo(state, obj) {
      state.redirectInfo = obj
    },
  },
  actions: {
    logout({ commit }) {
      commit('setAddress', {})
      commit('setUserInfo', {})
      commit('setToken', '')
    },
  },
}
